package com.yusong.algorithm.array;

/**
 * 快慢指针
 */
public class TwoPoint209 {


    public static void main(String[] args) {
        int[] nums = {2,3,1,2,4,3};
        TwoPoint209 main = new TwoPoint209();
        System.out.println(main.minSubArrayLen(7, nums));
    }


    public int minSubArrayLen(int s, int[] nums) {
        int length = nums.length;
        if(length == 0){
            return 0;
        }
        int leftIndex = 0;
        int rightIndex = -1;
        //不可能取到的最大值
        int maxLength = length+1;
        int sum = 0;
        while(leftIndex <length){
            if(sum < s){
                if(rightIndex<length-1){
                    rightIndex++;
                    sum+=nums[rightIndex];
                }else{
                    break;
                }
            }else{
                int currentLength = rightIndex - leftIndex + 1;
                maxLength = Math.min(maxLength, currentLength);
                sum-=nums[leftIndex];
                leftIndex++;
            }
        }
        return maxLength;
    }
}
